package com.CST.service.impl;

import cn.hutool.core.date.DateTime;
import com.CST.common.BaseEntity;
import com.CST.common.constant.Constants;
import com.CST.dto.InvoiceRecordsDTO;
import com.CST.entity.InvoiceRecords;
import com.CST.mapper.cst.InvoiceRecordsMapper;
import com.CST.service.IInvoiceRecordsService;
import com.CST.util.ExcelTransfer;
import com.CST.util.HttpUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 发票记录表 服务实现类
 * </p>
 *
 * @author qtx
 * @since 2022-08-31
 */
@Slf4j
@Service
public class InvoiceRecordsServiceImpl extends ServiceImpl<InvoiceRecordsMapper, InvoiceRecords> implements IInvoiceRecordsService {

    @Autowired
    private InvoiceRecordsMapper invoiceRecordsMapper;

    @Autowired
    private ExcelTransfer<InvoiceRecords> excelTransfer;

//    @Override
//    public IPage<InvoiceRecordsVo> findPage(InvoiceRecordsDto dto) {
//        return invoiceRecordsMapper.selectPageNew(new Page<>(dto.getNum(), dto.getSize()),
//                Wrappers.lambdaQuery(InvoiceRecords.class).eq(BaseEntity::getDeleteFlag, "0")
//                        .eq(dto.getMoney() != null, InvoiceRecords::getMoney, dto.getMoney())
//                        .like(StringUtils.isNotBlank(dto.getTicketCode()), InvoiceRecords::getTicketCode,
//                                dto.getTicketCode())
//                        .like(StringUtils.isNotBlank(dto.getTicketNumber()), InvoiceRecords::getTicketNumber,
//                                dto.getTicketNumber())
//                        .like(StringUtils.isNotBlank(dto.getProcessNumber()), InvoiceRecords::getProcessNumber,
//                                dto.getProcessNumber()).like(StringUtils.isNotBlank(dto.getReimbursementPerson()),
//                                InvoiceRecords::getReimbursementPerson, dto.getReimbursementPerson()));
//    }

    @Override
    public List<InvoiceRecords> findPage(InvoiceRecordsDTO dto) {
        return  invoiceRecordsMapper.selectList(Wrappers.lambdaQuery(InvoiceRecords.class).eq(BaseEntity::getDeleteFlag, "0")
                .eq(dto.getMoney() != null, InvoiceRecords::getMoney, dto.getMoney())
                .like(StringUtils.isNotBlank(dto.getTicketCode()), InvoiceRecords::getTicketCode,
                        dto.getTicketCode())
                .like(StringUtils.isNotBlank(dto.getTicketNumber()), InvoiceRecords::getTicketNumber,
                        dto.getTicketNumber())
                .like(StringUtils.isNotBlank(dto.getProcessNumber()), InvoiceRecords::getProcessNumber,
                        dto.getProcessNumber()).like(StringUtils.isNotBlank(dto.getReimbursementPerson()),
                        InvoiceRecords::getReimbursementPerson, dto.getReimbursementPerson()).orderByDesc(InvoiceRecords::getCreateTime));
    }

    @Override
    public boolean saveOrUpdateNew(InvoiceRecords info) {
        log.info("saveOrUpdateNew info:{}", info);
        InvoiceRecords one = getOne(
                Wrappers.lambdaQuery(InvoiceRecords.class).ne(info.getId() != null, BaseEntity::getId, info.getId())
                        .eq(StringUtils.isNotBlank(info.getTicketCode()), InvoiceRecords::getTicketCode,
                                info.getTicketCode())
                        .eq(StringUtils.isNotBlank(info.getTicketNumber()), InvoiceRecords::getTicketNumber,
                                info.getTicketNumber()));
        if (Objects.isNull(one)) {
            Date currDate = DateTime.now();
            info.setCreateTime(currDate);
            info.setUpdateTime(currDate);
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            info.setCreateBy(userName);
            info.setUpdateBy(userName);
            return saveOrUpdate(info);
        }
        return false;
    }

    @Override
    public void exportExcel(HttpServletResponse response) throws ClassNotFoundException {
        excelTransfer.exportExcel(response, list(), "发票记录表", "sheet", this);
    }
}
